package com.maiworld.content.service.impl;

import java.util.Date;
import java.util.List;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.maiworld.content.service.AnswerFeedbackService;
import com.maiworld.mapper.AnswerFeedbackMapper;
import com.maiworld.mapper.TbStudentMapper;
import com.maiworld.pojo.AnswerFeedback;
import com.maiworld.pojo.Subject;
import com.maiworld.pojo.TbStudent;

import entity.PageResult;
@Service
public class AnswerFeedbackServiceImpl implements AnswerFeedbackService{

	@Autowired
	private AnswerFeedbackMapper answerFeedbackMapper;
	
	@Autowired
	private TbStudentMapper tbStudentMapper;
	
	@Override
	public Long addAnswerFeedback(AnswerFeedback answerFeedback) {
		// TODO Auto-generated method stub
		answerFeedbackMapper.addAnswerFeedback(answerFeedback);
		return answerFeedback.getId();
	}

	@Override
	public int insertCorrect(List<Subject> correct) {
		// TODO Auto-generated method stub
		return answerFeedbackMapper.insertCorrect(correct);
	}

	@Override
	public int insertError(List<Subject> error) {
		// TODO Auto-generated method stub
		return answerFeedbackMapper.insertError(error);
	}

	@Override
	public List<AnswerFeedback> selectAnswerFeedback(AnswerFeedback answerFeedback) {
		// TODO Auto-generated method stub
		return answerFeedbackMapper.selectAnswerFeedback(answerFeedback);
	}

	@Override
	public List<Subject> selectQuestion(Subject sub) {
		// TODO Auto-generated method stub
		return answerFeedbackMapper.selectQuestion(sub);
	}

	@Override
	public int addAnswerFeedbackL(Long sid, String subject, String status) {
		// TODO Auto-generated method stub
		AnswerFeedback answerFeedback = new AnswerFeedback();
		TbStudent tbStudent = tbStudentMapper.selectByPrimaryKey(sid);
		
		answerFeedback.setSid(sid);
		answerFeedback.setSubject(subject);
		answerFeedback.setStatus(status);
		
		AnswerFeedback res = answerFeedbackMapper.selectAnswerFeedbackL(answerFeedback);

		
		if(res != null) {
			res.setCreated(new Date());
			return answerFeedbackMapper.updateAnswerFeedbackL(res);
		}else {
			answerFeedback.setPhone(tbStudent.getPhone());
			answerFeedback.setName(tbStudent.getName());
			answerFeedback.setType("1");
			answerFeedback.setCreated(new Date());
			return answerFeedbackMapper.addAnswerFeedbackL(answerFeedback);
		}	
			
	}

	@Override
	public PageResult searchAnswerFeedbackList(AnswerFeedback answerFeedback,int page,int rows) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, rows);
		Page<AnswerFeedback> pageList = (Page<AnswerFeedback>)answerFeedbackMapper.searchAnswerFeedbackL(answerFeedback);
		return new PageResult(pageList.getTotal(), pageList.getResult());
	}

	@Override
	public int updateAnswerFeedback(AnswerFeedback answerFeedback) {
		// TODO Auto-generated method stub
		return answerFeedbackMapper.updateAnswerFeedbackL(answerFeedback);
	}



}
